package com.sinosoft.lis.schema;

import java.io.Serializable;

import android.database.Cursor;

import com.sinosoft.lis.db.DisplayDB;
import com.sinosoft.utility.CError;
import com.sinosoft.utility.CErrors;
import com.sinosoft.utility.Schema;
import com.sinosoft.utility.StrTool;
import com.sinosoft.utility.SysConst;

public class DisplaySchema implements Schema, Cloneable, Serializable{

	
	private static final long serialVersionUID = 1L;
	
	//field
	//标题,textView 显示的内容
	private String Title;
	
	//类型
	private String Type;
	
	//文件名
	private String FileName;
	
	private static final int FIELDNUM = 3;
	
	private static String[] pk;
	
	public CErrors mErrors;
	
	
	public DisplaySchema(){
		mErrors = new CErrors();
		
		String[] pk = new String[1];
		pk[0] = "Title";
		
		pk = pk;
	}
	
	
	public Object clone() throws CloneNotSupportedException{
		DisplaySchema cloned = (DisplaySchema)super.clone();
		cloned.mErrors = (CErrors) mErrors.clone();
		return cloned;
	}
	
	@Override
	public String[] getPK() {
		// TODO Auto-generated method stub
		return pk;
	}
	
	
	public String getTitle() {
		return Title;
	}


	public void setTitle(String title) {
		Title = title;
	}


	public String getType() {
		return Type;
	}


	public void setType(String type) {
		Type = type;
	}


	public String getFileName() {
		return FileName;
	}


	public void setFileName(String fileName) {
		FileName = fileName;
	}

	
	public void setSchema(DisplaySchema alDisplaySchema){
		this.Title = alDisplaySchema.getTitle();
		this.Type = alDisplaySchema.getType();
		this.FileName = alDisplaySchema.getFileName();
	}
	
	
	public boolean setSchema(Cursor cursor,int i){
		boolean flag = true;
		try
		{
			if (cursor.getString(cursor.getColumnIndex("Title")) == null )
				this.Title = null;
			else
				this.Title = cursor.getString(cursor.getColumnIndex("Title")).trim();

			if (cursor.getString(cursor.getColumnIndex("Type")) == null )
				this.Type = null;
			else
				this.Type = cursor.getString(cursor.getColumnIndex("Type")).trim();

			if (cursor.getString(cursor.getColumnIndex("FileName")) == null )
				this.FileName = null;
			else
				this.FileName = cursor.getString(cursor.getColumnIndex("FileName")).trim();

		}
		catch (Exception e) 
		{
			flag = false;
		}
		return flag;
	}
	
	
	public DisplaySchema getSchema(){
		DisplaySchema aDisplaySchema = new DisplaySchema();
		aDisplaySchema.setSchema(this);
		return aDisplaySchema;
	}
	
	public DisplayDB getDB(){
		DisplayDB aDBOper = new DisplayDB();
		aDBOper.setSchema(this);
		return aDBOper;
	}
	
	
	public String encode()
	{
		StringBuffer strReturn = new StringBuffer(256);
		strReturn.append(StrTool.cTrim(Title)); strReturn.append(SysConst.PACKAGESPILTER);
		strReturn.append(StrTool.cTrim(Type)); strReturn.append(SysConst.PACKAGESPILTER);
		strReturn.append(StrTool.cTrim(FileName)); strReturn.append(SysConst.PACKAGESPILTER);
		
		return strReturn.toString();
	}

	
	
	public boolean decode(String strMessage)
	{
		try
		{
			Title = StrTool.getStr(StrTool.GBKToUnicode(strMessage), 1, SysConst.PACKAGESPILTER );
			Type = StrTool.getStr(StrTool.GBKToUnicode(strMessage), 2, SysConst.PACKAGESPILTER );
			FileName = StrTool.getStr(StrTool.GBKToUnicode(strMessage), 3, SysConst.PACKAGESPILTER );
			
		}
		catch(NumberFormatException ex)
		{
			// @@������
			CError tError = new CError();
			tError.moduleName = "DisplaySchema";
			tError.functionName = "decode";
			tError.errorMessage = ex.toString();
			this.mErrors.addOneError(tError);

			return false;
		}
		return true;
	}
	

	@Override
	public String getV(String FCode) {
		// TODO Auto-generated method stub
		String strReturn = "";
		if (FCode.equalsIgnoreCase("Title"))
		{
			strReturn = StrTool.GBKToUnicode(String.valueOf(Title));
		}
		if (FCode.equalsIgnoreCase("Type"))
		{
			strReturn = StrTool.GBKToUnicode(String.valueOf(Type));
		}
		if (FCode.equalsIgnoreCase("FileName"))
		{
			strReturn = StrTool.GBKToUnicode(String.valueOf(FileName));
		}
		
		if (strReturn.equals(""))
		{
			strReturn = "null";
		}

		return strReturn;
	}

	@Override
	public String getV(int nIndex) {
		// TODO Auto-generated method stub
		String strFieldValue = "";
		switch(nIndex) {
			case 0:
				strFieldValue = StrTool.GBKToUnicode(Title);
				break;
			case 1:
				strFieldValue = StrTool.GBKToUnicode(Type);
				break;
			case 2:
				strFieldValue = StrTool.GBKToUnicode(FileName);
				break;
			
			default:
				strFieldValue = "";
		};
		if( strFieldValue.equals("") ) {
			strFieldValue = "null";
		}
		return strFieldValue;
	}

	@Override
	public int getFieldType(String strFieldName) {
		// TODO Auto-generated method stub
		if( strFieldName.equals("Title") ) {
			return Schema.TYPE_STRING;
		}
		if( strFieldName.equals("Type") ) {
			return Schema.TYPE_STRING;
		}
		if( strFieldName.equals("FileName") ) {
			return Schema.TYPE_STRING;
		}
		
		return Schema.TYPE_NOFOUND;
	}

	@Override
	public int getFieldType(int nFieldIndex) {
		// TODO Auto-generated method stub
		int nFieldType = Schema.TYPE_NOFOUND;
		switch(nFieldIndex) {
			case 0:
				nFieldType = Schema.TYPE_STRING;
				break;
			case 1:
				nFieldType = Schema.TYPE_STRING;
				break;
			case 2:
				nFieldType = Schema.TYPE_STRING;
				break;
			case 3:
				nFieldType = Schema.TYPE_STRING;
				break;
			case 4:
				nFieldType = Schema.TYPE_STRING;
				break;
			case 5:
				nFieldType = Schema.TYPE_STRING;
				break;
			default:
				nFieldType = Schema.TYPE_NOFOUND;
		};
		return nFieldType;
	}

	@Override
	public int getFieldCount() {
		// TODO Auto-generated method stub
		return FIELDNUM;
	}

	@Override
	public int getFieldIndex(String strFieldName) {
		// TODO Auto-generated method stub
		if( strFieldName.equals("Title") ) {
			return 0;
		}
		if( strFieldName.equals("Type") ) {
			return 1;
		}
		if( strFieldName.equals("FileName") ) {
			return 2;
		}
		
		return -1;
	}

	@Override
	public String getFieldName(int nFieldIndex) {
		// TODO Auto-generated method stub
		String strFieldName = "";
		switch(nFieldIndex) {
			case 0:
				strFieldName = "Title";
				break;
			case 1:
				strFieldName = "Type";
				break;
			case 2:
				strFieldName = "FileName";
				break;
			
			default:
				strFieldName = "";
		};
		return strFieldName;
	}

	@Override
	public boolean setV(String strFieldName, String strFieldValue) {
		// TODO Auto-generated method stub
		if( StrTool.cTrim( strFieldName ).equals( "" ))
			return false;

		if (strFieldName.equalsIgnoreCase("Title"))
		{
			if( strFieldValue != null && !strFieldValue.equals(""))
			{
				Title = strFieldValue.trim();
			}
			else
				Title = null;
		}
		if (strFieldName.equalsIgnoreCase("Type"))
		{
			if( strFieldValue != null && !strFieldValue.equals(""))
			{
				Type = strFieldValue.trim();
			}
			else
				Type = null;
		}
		if (strFieldName.equalsIgnoreCase("FileName"))
		{
			if( strFieldValue != null && !strFieldValue.equals(""))
			{
				FileName = strFieldValue.trim();
			}
			else
				FileName = null;
		}
		
		return true;
	}
	
	/*public boolean equals(Object otherObject)
	{
		if (this == otherObject) return true;
		if (otherObject == null) return false;
		if (getClass() != otherObject.getClass()) return false;
		LDCodeSchema other = (LDCodeSchema)otherObject;
		return
			Title.equals(other.getTitle())
			&& Type.equals(other.getType())
			&& FileName.equals(other.getFileName());
	}*/

}
